<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="isCheckedAll" :disabled="Disable"/>
    </label>
    <span> <span>已完成 {{ NumberOfCompletedItems }} </span> / 全部 {{ todosLength }} </span>
    <button class="btn btn-danger" @click="deleteAll">清除已完成任务</button>
  </div>
</template>

<script lang="ts">
import { defineComponent,computed, } from "vue";

export default defineComponent({
  name: "Footer",
});
</script>

<script setup lang="ts">
import {todosType} from '../../App.vue'
  const props = defineProps<{
    todos:todosType
  }>()
  const emit = defineEmits<{
    (event:'SelectAll',commentType:boolean):void
    (event:'deleteTodosAll'):void
  }>()
  //总长度
  const todosLength = computed(()=>{
    return props.todos.length
  })
  //完成个数
  const NumberOfCompletedItems = computed(()=>{
    return props.todos.reduce((p,c)=>p + (c.comment ? 1 : 0),0)
  })
  //禁用
  const Disable = computed(()=>{
    return props.todos.length <= 0
  })
  //任务全选中就选中
  const isCheckedAll = computed({
    get(){
      return(
        /*第一种方法 NumberOfCompletedItems.value === props.todos.length && props.todos.length !== 0 */
        //第二种方法
        props.todos.every(item=>item.comment) && props.todos.length !== 0 
      )
    },
    set(commentType:boolean){
      emit('SelectAll',commentType)
    }
  })
  //删除全部完成的任务
  const deleteAll = ()=>{
    emit('deleteTodosAll')
  }
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
  &:disabled{
    cursor: not-allowed;
  }
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
